/*
 * (C) Copyright 2011 Adobe Systems Incorporated. All Rights Reserved.
 *
 * NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the
 * terms of the Adobe license agreement accompanying it.  If you have received this file from a
 * source other than Adobe, then your use, modification, or distribution of it requires the prior
 * written permission of Adobe.
 * THIS CODE AND INFORMATION IS PROVIDED "AS-IS" WITHOUT WARRANTY OF
 * ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 *
 *  THIS CODE IS NOT SUPPORTED BY Adobe Systems Incorporated.
 *
 */

package
{
	import flash.display.MovieClip;
	import flash.events.ActivityEvent;
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	import flash.media.Camera;
	import flash.media.Microphone;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.text.TextField;

	public class LiveStreams extends MovieClip
	{
		public var nc:NetConnection;

		public var ns:NetStream;

		public var video:Video;

		public var camera:Camera;

		public var mic:Microphone;

		public function LiveStreams()
		{
			var startBtn:TextField = new TextField();
			startBtn.textColor = 0xff0000;
			startBtn.name = "start";
			startBtn.text = "start";
			addChild(startBtn);

			var stopBtn:TextField = new TextField();
			stopBtn.name = "stop";
			stopBtn.text = "stop";
			stopBtn.textColor = 0xff0000;
			addChild(stopBtn);
			stopBtn.y = 100;

			startBtn.addEventListener(MouseEvent.CLICK, startHandler);
			stopBtn.addEventListener(MouseEvent.CLICK, stopHandler);
		}

		/*
		 *  Connect and start publishing the live stream
		 */
		private function startHandler(event:MouseEvent):void
		{
			nc = new NetConnection();
			nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			nc.connect("rtmp://localhost/live");
		}


		/*
		 *  Disconnect from the server
		 */
		private function stopHandler(event:MouseEvent):void
		{
			trace("Now we're disconnecting");
			nc.close();
		}

		private function netStatusHandler(event:NetStatusEvent):void
		{
			trace(event.info.code);
			switch (event.info.code)
			{
				case "NetConnection.Connect.Success":
					publishLiveStream();
					break;
			}
		}


		private function activityHandler(event:ActivityEvent):void
		{
			trace("activityHandler: " + event);
			trace("activating: " + event.activating);
		}

		/*
		 *  Create a live stream, attach the camera and microphone, and
		 *  publish it to the local server
		 */
		private function publishLiveStream():void
		{
			ns = new NetStream(nc);
			ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);

			camera = Camera.getCamera();
			mic = Microphone.getMicrophone();

			if (camera != null)
			{

				camera.addEventListener(ActivityEvent.ACTIVITY, activityHandler);

				video = new Video();
				video.attachCamera(camera);

				ns.attachCamera(camera);

				addChild(video);
			}

			if (mic != null)
			{
				mic.addEventListener(ActivityEvent.ACTIVITY, activityHandler);

				ns.attachAudio(mic);
			}

			if (camera != null || mic != null)
			{
				// start publishing
				// triggers NetStream.Publish.Start
				ns.publish("localnews", "live");
			}
			else
			{
				trace("Please check your camera and microphone");
			}
		}
	}
}

